package dispinfo.gismeteo;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import dispinfo.Lib;

public class GismeteoDataItem
{
	private static Image[] images;
	private static int itemHeight;
	private static int dateWidth;
	private static Font smallFont;
	private static Font boldFont;
	
	private final static int TOD_NIGHT   = 0;
	private final static int TOD_MORNING = 1;
	private final static int TOD_DAY     = 2;
	private final static int TOD_EVENING = 3;
	
	private final static int CLOUD_FINE   = 0;
	private final static int CLOUD_SOME   = 1;
	private final static int CLOUD_CLOUDY = 2;
	private final static int CLOUD_DULL   = 3;
	
	private final static int PREC_RAIN   = 4;
	private final static int PREC_SHOWER = 5;
	private final static int PREC_SNOW1  = 6;
	private final static int PREC_SNOW2  = 7;
	private final static int PREC_STORM  = 8;
	private final static int PREC_CLEAR  = 10;
	
	private static final int IMG_CLEAR      = 0;
	private static final int IMG_DULL       = 1;
	private static final int IMG_FEW_CLOUDS = 2;
	private static final int IMG_OVERCAST   = 3;
	private static final int IMG_RAIN       = 4;
	private static final int IMG_SHOWERS    = 5;
	private static final int IMG_LIGHTNING  = 6;
	private static final int IMG_SNOW       = 7;
	private static final int IMG_WIND_E     = 8;
	private static final int IMG_WIND_N     = 9;
	private static final int IMG_WIND_NE    = 10;
	private static final int IMG_WIND_NW    = 11;
	private static final int IMG_WIND_S     = 12;
	private static final int IMG_WIND_SE      = 13;
	private static final int IMG_WIND_SW      = 14;
	private static final int IMG_WIND_W       = 15;
	private static final int IMG_CLEAR_N      = 16;
	private static final int IMG_FEW_CLOUDS_N = 17;
	
	// Дата
	private int day;
	private int mon;
	
	// время суток, для которого составлен прогноз: 0 - ночь 1 - утро, 2 - день, 3 - вечер
	private int tod;
	
	//облачность по градациям: 0 - ясно, 1- малооблачно, 2 - облачно, 3 - пасмурно
	private int cloudiness;
	
	// тип осадков: 4 - дождь, 5 - ливень, 6,7 – снег, 8 - гроза, 9 - нет данных, 10 - без осадков
	private int precipitation;
	
	// Мин и макс. температура
	private int minTemp;
	private int maxTemp;
	
	// Ветер
	private int minWind;
	private int maxWind;
	private int windDir;
	
	static
	{
		try
		{
			images = new Image[]{ 
				Image.createImage("/gismeteo/cl-clear.png"),
				Image.createImage("/gismeteo/cl-dull.png"),
				Image.createImage("/gismeteo/cl-few_clouds.png"),
				Image.createImage("/gismeteo/cl-overcast.png"),
				Image.createImage("/gismeteo/pr-rain.png"),
				Image.createImage("/gismeteo/pr-showers.png"),
				Image.createImage("/gismeteo/pr-lightning.png"),
				Image.createImage("/gismeteo/pr-snow.png"),
				Image.createImage("/gismeteo/wind_e.png"),
				Image.createImage("/gismeteo/wind_n.png"),
				Image.createImage("/gismeteo/wind_ne.png"),
				Image.createImage("/gismeteo/wind_nw.png"),
				Image.createImage("/gismeteo/wind_s.png"),
				Image.createImage("/gismeteo/wind_se.png"),
				Image.createImage("/gismeteo/wind_sw.png"),
				Image.createImage("/gismeteo/wind_w.png"),
				Image.createImage("/gismeteo/cl-clear2.png"),
				Image.createImage("/gismeteo/cl-few_clouds2.png"),
			};
			
			int maxImgHeight = 0;
			for (int i = 0; i < images.length; i++)
			{
				int height = images[i].getHeight();
				if (height > maxImgHeight) maxImgHeight = height;
			}
			
			smallFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
			boldFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);
			
			dateWidth = smallFont.stringWidth("######");
			
		
			int textHeight = smallFont.getHeight() + boldFont.getHeight();
			
			if (textHeight > maxImgHeight)
			{
				for (int i = 0; i < images.length; i++)
				{
					int newWidth = images[i].getWidth()*textHeight/images[i].getHeight();
					images[i] = Lib.resizeImage(images[i], newWidth, textHeight, true);
				}
			}

			if (textHeight > maxImgHeight) itemHeight = textHeight;
			
			
			else itemHeight = maxImgHeight;
			itemHeight = 5*itemHeight/4;
			
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public GismeteoDataItem(int day, int mon, int tod, int cloudiness, int precipitation, 
			int minTemp, int maxTemp, int minWind, int maxWind, int windDir)
	{
		this.day = day;
		this.mon = mon; 
		this.tod = tod;
		this.cloudiness = cloudiness;
		this.precipitation = precipitation;
		this.minTemp = minTemp;
		this.maxTemp = maxTemp;
		this.minWind = minWind;
		this.maxWind = maxWind;
		this.windDir = windDir;
	}
	
	public int getHeight()
	{
		return itemHeight;
	}
	
	private static String Int02ToStr(int value)
	{
		return (value < 10) ? "0"+Integer.toString(value) : Integer.toString(value);
	}
	
	private String getDateString()
	{
		return Int02ToStr(day)+"."+Int02ToStr(mon);
	}
	
	private String getTodString()
	{
		switch (tod)
		{
		case TOD_NIGHT:   return "Ночь";
		case TOD_MORNING: return "Утро";
		case TOD_DAY:     return "День";
		case TOD_EVENING: return "Вечер";
		}
		return null;
	}
	
	private String getTempString()
	{
		return Integer.toString(minTemp)+"..."+Integer.toString(maxTemp)+"°C";
	}
	
	private Image getCloudsImage()
	{
		switch (cloudiness)
		{
		case CLOUD_FINE:   return images[tod != TOD_NIGHT ? IMG_CLEAR : IMG_CLEAR_N];
		case CLOUD_SOME:   return images[tod != TOD_NIGHT ? IMG_FEW_CLOUDS : IMG_FEW_CLOUDS_N];
		case CLOUD_CLOUDY: return images[IMG_OVERCAST];
		case CLOUD_DULL:   return images[IMG_DULL];
		}
		return null;
	}
	
	private Image getPercImage()
	{
		switch (precipitation)
		{
		case PREC_RAIN:         return images[IMG_RAIN];
		case PREC_SHOWER: return images[IMG_SHOWERS];
		case PREC_STORM:        return images[IMG_LIGHTNING];
		case PREC_SNOW1:
		case PREC_SNOW2:		return images[IMG_SNOW];
		case PREC_CLEAR:        return null;
		}
		
		return null;
	}
	
	private String getWindString()
	{
		if ((minWind == 0) && (maxWind == 0)) return "Штиль";
		return Integer.toString(minWind)+"..."+Integer.toString(maxWind)+" м/с";
	}
	
	private Image getWindImage()
	{
		switch (windDir)
		{
		case 0: return images[IMG_WIND_N];
		case 1: return images[IMG_WIND_NE];
		case 2: return images[IMG_WIND_E];
		case 3: return images[IMG_WIND_SE];
		case 4: return images[IMG_WIND_S];
		case 5: return images[IMG_WIND_SW];
		case 6: return images[IMG_WIND_W];
		case 7: return images[IMG_WIND_NW];
		
		}
		return null;
	}
	
	public void paint(Graphics g, int x, int y, int width, int height)
	{
		int y1 = y;
		int y2 = y+height;
		int smallFontHeight = smallFont.getHeight();
		
		g.setColor(0);
		g.setFont(smallFont);
		
		// Выводим дату и время суток
		int strX1 = x;
		int strX2 = x+dateWidth;
	
		final String todString = getTodString();
		int todStringWidth = boldFont.stringWidth(todString);
		g.setFont(boldFont);
		g.drawString(todString, (strX1+strX2-todStringWidth)/2, (y1+y2-2*smallFontHeight)/2, Graphics.TOP|Graphics.LEFT);
		
		final String dateString = getDateString();
		int dateStringWidth = smallFont.stringWidth(dateString);
		g.setFont(smallFont);
		g.drawString(dateString, (strX1+strX2-dateStringWidth)/2, (y1+y2-2*smallFontHeight)/2+smallFontHeight, Graphics.TOP|Graphics.LEFT);
		
		x += dateWidth;
		
		// Выводим картинку с погодой
		final Image clImage = getCloudsImage();
		final Image precImage = getPercImage();
		
		int clImgWidth = (clImage != null) ? clImage.getWidth() : 0;
		int clImgHeight = (clImage != null) ? clImage.getHeight() : 0;
		int precImgWidth = (precImage != null) ? precImage.getWidth() : 0;
		int precImgHeight = (precImage != null) ? precImage.getHeight() : 0;
		
		int imgWidth = (clImgWidth > precImgWidth) ? clImgWidth : precImgWidth;
		int imgHeight = (clImgHeight > precImgHeight) ? clImgHeight : precImgHeight;
		
		if (precImage != null)
			g.drawImage(precImage, x+(imgWidth-precImgWidth)/2, (y1+y2-precImgHeight)/2, Graphics.TOP|Graphics.LEFT);
		
		if (clImage != null)
			g.drawImage(clImage, x+(imgWidth-clImgWidth)/2, (y1+y2-clImgHeight)/2, Graphics.TOP|Graphics.LEFT);
		
	
		x += imgWidth+2;
		
		// Выводим температуру
		g.setFont(boldFont);
		final String tempStr = getTempString();
		int tempStrWidht = boldFont.stringWidth(tempStr);
		g.drawString(tempStr, x, (y1+y2-2*smallFontHeight)/2, Graphics.TOP|Graphics.LEFT);
		
		// Выводим скорость ветра
		g.setFont(smallFont);
		final String windStr = getWindString();
		int windStrWidth = smallFont.stringWidth(windStr);
		g.drawString(windStr, x, (y1+y2-2*smallFontHeight)/2+smallFontHeight, Graphics.TOP|Graphics.LEFT);
		
		int windOrTempWidth = (windStrWidth > tempStrWidht) ? windStrWidth : tempStrWidht;   
		
		x += windOrTempWidth+2;
		
		// Показываем направление ветра
		if (maxWind != 0)
		{
			final Image imgWind = getWindImage();
			if (imgWind != null)
			{
				int imgWindWidth = imgWind.getWidth();
				int imgWindHeight = imgWind.getHeight();
				g.drawImage(imgWind, x, (y1+y2-imgWindHeight)/2, Graphics.TOP|Graphics.LEFT);
				x += imgWindWidth;
			}
		}
		
	}
	
}
